<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */

class Action extends admin_abs
{
	function index()
	{
	    $data = _model('user')->getList("order by id desc", new Pager(20));
	    Smarty3::instance()->assign($data);
	    Smarty3::instance()->display('web_user.html');
	}
	
	function create()
	{
	    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	        $info = _POST('info', array());
	        if ($info['password'] && $info['name']) {
	            $name = _model('user')->read(array('name'=>$info['name']));
				$email = _model('user')->read(array('email' => $info['email']));
				if($email){
					
					msg("email已存在");
					
				}
				if(filter_var($info['email'], FILTER_VALIDATE_EMAIL) === false){
					
					msg("email格式不正确");
					
				}
	            if (!$name) {
	                $info['password'] = md5($info['password']);
					$info['add_time'] = time();
				if(!strip_tags($info['name'])){
					
					msg('用胡名不能为空');
					
				}
        	        _model('user')->create($info);
        	        redirect('?url=web_user');
	            } else {
	               msg('用户名存在');
	            }
	        } else {
	           msg('用户名和密码不能为空');
	        }
        } else {
    	    Smarty3::instance()->display('web_user_create.html');
        }
	}
	
	function update()
	{
	    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	        $info = _POST('info', array());
	        $id = _POST('id', 0);
	        if ($info['name']) {
	            if (!$name = _model('user')->read("WHERE `id` != $id AND `name`= '$info[name]'")) {
	                if ($info['password']) {
    	                $info['password'] = md5($info['password']);
	                } else {
	                   unset($info['password']);
	                }	
					if(filter_var($info['email'], FILTER_VALIDATE_EMAIL) === false){
						msg("email格式不对");
					}
					if(!strip_tags($info['name'])){
						
						
						msg('用户名不能为空');
						
					}
					$email=_model('user')->read("WHERE id<>$id AND email='{$info[email]}'");
					if($email){
						
						msg("EMAIL存在");
					}
        	        _model("user")->update(array('id'=>$id), $info);
        	        redirect('?url=web_user');
	            } else {
	               msg('用户名存在');
	            }
	        } else {
	           msg('用户名和密码不能为空');
	        }
	    } else {
	        $id = _GET('id', 0);
	        if ($id && $data = _model("user")->read(array('id'=>$id))) {
        	    Smarty3::instance()->assign('data', $data);
        	    Smarty3::instance()->display('web_user_update.html');
	        }
	    }
	}
	
    function delete()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id_array = array_filter(_POST('check', array()));
			$new_id_array=implode(",",$id_array);
             _model('user')->delete("where id in(".$new_id_array.")");
            redirect('?url=web_user');
        } else {
            $id = _GET('id', 0);
            if ($id && $admin_info = _model('user')->read(array('id'=>$id))) {
                _model('user')->delete(array('id'=>$id));
                redirect('?url=web_user.html');
            }
        }
    }
}

?>
